﻿using System;
using AspectCore.Configuration;
using AspectCore.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NetMicro.Core.Ioc.Extensions;

namespace NetMicro.Core.Ioc
{
    /// <summary>
    /// 系统注入扩展
    /// </summary>
    public static class DependencyHostBuilderExtensions
    {
        /// <summary>
        /// 系统统一注入基类相关
        /// </summary>
        /// <param name="hostBuilder"></param>
        /// <returns></returns>
        public static IHostBuilder UseDependencyProvider(this IHostBuilder hostBuilder, Action<IAspectConfiguration> configure = null)
        {
            if (hostBuilder == null)
            {
                throw new ArgumentNullException(nameof(hostBuilder));
            }
            hostBuilder
                .ConfigureServices((hostingContext, service) =>
                {
                    //aspect aop
                    service.ConfigureDynamicProxy(configure);
                    //系统默认服务
                    service.AddDefaltService();
                }).UseServiceProviderFactory(new DependencyServiceProviderFactory());
            return hostBuilder;
        }

    }
}
